 package com.certificationManager.client.presenter;

import com.certificationManager.client.CMEventBus;
import com.certificationManager.client.bean.UserBean;
import com.certificationManager.client.presenter.view_interface.IRegistrationView;
import com.certificationManager.client.service.UserServiceAsync;
import com.certificationManager.client.view.RegistrationView;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mvp4g.client.annotation.InjectService;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter( view = RegistrationView.class )
public class RegistrationPresenter extends BasePresenter<IRegistrationView, CMEventBus>{

	private UserServiceAsync service = null;
	
	@InjectService
	public void setUserService( UserServiceAsync service ) {
		this.service = service;
	}
	
	private UserBean user = null;
	
	@Override
	public void bind(){

		view.getBackButton().addListener(Events.OnClick, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				eventBus.goToLogin();
			}
		});
		
		view.getRegistrationButton().addListener(Events.OnClick, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				fillBean();
				createUser();
			}
		});
		
	}
	
	public void onGoToRegistration() {
		init();
		eventBus.changeBody(view.getViewWidget());
	}

	private void fillBean() {
		user.setFirstName( view.getFirstnameField().getValue() );
		user.setLastName( view.getLastnameField().getValue() );
		user.setEmail( view.getEmailField().getValue() );
		user.setPassword( view.getPassField().getValue() );
		//TODO: et la langue !!!!
	}
	
	private void createUser() {
		
		if(
				// All cases are tested because isValid put graphical warning in the field.
				!view.getFirstnameField().isValid() |
				!view.getLastnameField().isValid() |
				!view.getEmailField().isValid() |
				!view.getPassField().isValid() |
				!view.getPassBisField().isValid()) {
			MessageBox.alert("Alert", "error", null);
			return;
		}

		final MessageBox box = MessageBox.wait("Progress", "Inscription en cours...", "Saving...");
		
		service.register( user, new AsyncCallback<Void>() {
			@Override
			public void onFailure( Throwable caught ) {
				box.close();
				MessageBox.alert("Alert", caught.getLocalizedMessage(), null); 
			}
			@Override
			public void onSuccess( Void result ) {
				init();
				Info.display("Message", "Votre compte est créé.", "");  
				box.close();
				eventBus.goToLogin();
			}
		});
	}

	private void init() {
		user = new UserBean();
		
		view.getFirstnameField().clear();
		view.getLastnameField().clear();
		view.getEmailField().clear();
		view.getPassField().clear();
		view.getPassBisField().clear();
	}
	
}
